package edu.gatech.app4args.utils;

import javax.swing.tree.DefaultMutableTreeNode;

/**
 * Used to store a CompendiumNode as the userObject. Extra methods were added
 * to determine if the tree node will have children, and if it is equal to
 * tree node based on a comparison of their CompendiumNodes.
 * 
 * @author Andrew Lunsford
 */
public class CustomTreeNode extends DefaultMutableTreeNode {

	/**
	 * Creates a CustomTreeNode, setting userObject to a CompendiumNode.
	 *
	 * @param node the node to store in the tree node
	 */
	public CustomTreeNode(CompendiumNode node){
		this.userObject = node;
	}

	/**
	 * Used to retrieve the userObject as a CompendiumNode.
	 *
	 * @return the tree node's CompendiumNode
	 */
	public CompendiumNode getCNode(){
		return (CompendiumNode)userObject;
	}

	/**
	 * Determines if the tree node child exists. I've actually forgotten when
	 * this is used, or what specifically for. Oops...
	 *
	 * @param child the tree node to check
	 * @return true if the child exists
	 */
	public boolean childExists(CustomTreeNode child){
		boolean found = false;
		int i = 0;
		while(i < this.getChildCount()){
			if(((CustomTreeNode)this.getChildAt(i)).equals(child)){
				found = true;
			}
			i++;
		}
		return found;
	}

	/**
	 * Compares tree nodes based on their CompendiumNodes.
	 * 
	 * @param aThat the other tree node to compare to
	 * @return true if they are equal
	 */
	@Override
	public boolean equals(Object aThat){
		if(this == aThat) return true;
		if(!(aThat instanceof CustomTreeNode)) return false;
		CustomTreeNode that = (CustomTreeNode)aThat;
		return ((CompendiumNode)userObject).equals(that.getUserObject());
	}

	/**
	 * Calculates the hashCode for the tree node using the CompendiumNode.
	 * @return
	 */
	@Override
	public int hashCode(){
		return ((CompendiumNode)userObject).hashCode();
	}
}